package com.lh.cookie与session的研究;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * 作者：idea
 * 日期：2018/7/2
 * 描述：打印出已经登录用户的信息
 */
@WebServlet(name = "/userList",urlPatterns = "/userList")
public class UserMessHandle extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out=resp.getWriter();
        HttpSession session=req.getSession();
        Cookie[] cookies=req.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("uuid-token")) {
                    String key = cookie.getValue();
                    System.out.println(key);
                    User user = (User) session.getAttribute(key);  //模拟存储的用户信息
                    out.print(user.toString());
                    break;
            }
        }
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
